package com.company4.copy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
    public static void copyFile(String oldPath, String newPath) {
        // 使用缓冲流进行文件拷贝
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            // 被拷贝文件
            in = new BufferedInputStream(new FileInputStream(oldPath));
            // 拷贝的文件
            out = new BufferedOutputStream(new FileOutputStream(newPath));
            byte[] arr = new byte[8 * 1024];
            int res = 0;
            // 拷贝
            while ((res = in.read(arr)) != -1) {
                out.write(arr, 0, res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
